/*
    Copyright (C) 2013  Letian Yi  <letian0805@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "chat.h"

static const char *g_head[HEAD_LAST] = {
    [HEAD_START] = "START",
    [HEAD_TO] = "TO:",
    [HEAD_FROM] = "FROM:",
    [HEAD_MSG] = "MSG:",
    [HEAD_TIME] = "TIME:",
    [HEAD_FDBK] = "FDBK:",
    [HEAD_END] = "END",
};

char *str_strip(char *str)
{
    if (str==NULL){
        return NULL;
    }
    int i=0;
    int len = strlen(str);
    if (str[len-1]=='\n'){
        str[len-1]='\0';
        len--;
    }
    if (str[0]!=' '  && str[len-1]!=' '){
        return str;
    }
    while( str[i]==' '){
        i++;
        if (str[i]=='\0')
            return NULL;
    }
    str += i;
    len -= i;
    while( str[len-1]==' '){
        len--;
    }
    str[len] = '\0';

    return str;
}

int line_parse(char *line, char tok,  char **key, char **val)
{
    if (line){
        line = str_strip(line);
    }
    if (line==NULL || key==NULL || val==NULL){
        return -1;
    }

    *key = NULL;
    *val = NULL;

    char *s = strchr(line, tok);
    if (s == NULL){
        return -1;
    }

    *s = '\0';
    s++;
    *key = str_strip(line);
    *val = str_strip(s);

    return 0;
}

ChatPacket *packet_new(const char *from, const char *to)
{
    ChatPacket *pkt = (ChatPacket *)malloc(sizeof(ChatPacket));
    bzero(pkt, sizeof(ChatPacket));

    if (from){
        pkt->from = strdup(from);
    }
    if (to){
        pkt->to = strdup(to);
    }

    return pkt;
}

int packet_add_msg1(ChatPacket *pkt, const char *msg)
{
    if (pkt->nmsg>=MAXMSG || msg==NULL){
        return -1;
    }
    if (!strncmp(msg, CMD_WHOISON_STR, sizeof(CMD_WHOISON_STR) - 1)) {
        msg+=sizeof(CMD_WHOISON_STR) - 1 ;
        
    }
    pkt->msg[pkt->nmsg] = strdup(msg);
    pkt->nmsg++;

    return 0;
}

HeadType packet_head_parse(const char *head)
{
    if (head==NULL || *head=='\0'){
        return HEAD_LAST;
    }

    HeadType type;
    for (type = 0; type<HEAD_LAST; type++){
        int len = strlen(g_head[type]);
        if(strncmp(head, g_head[type], len)==0){
            return type;
            break;
        }
    }

    return HEAD_LAST;
}

char *packet_head_getdata(HeadType type, char *head)
{

    int len = strlen(g_head[type]);

    char *str = head + len;
    while(*str==' ')
        str++;
    return str;       
}

char *packet_head_make(HeadType type, const char *data)
{
    if (data==NULL){
        return NULL;
    }
    char buf[MAXLEN] = {0};

    sprintf(buf, " %s%s\n", g_head[type],data);

    return strdup(buf);
}

int socket_send_head(int sktfd, HeadType type, const char *data)
{
    char buf[MAXLEN];
    int i = 0;

    if (type==HEAD_START || type==HEAD_END){
        i = snprintf(buf, MAXLEN, "%s\n", g_head[type]);
    }else if (((data==NULL) || (*data=='\0')) || type>=HEAD_LAST){
        return 0;
    }else{
        i = snprintf(buf, MAXLEN, "%s%s\n", g_head[type], data);
    }
    
    if (socket_writeline(sktfd, buf, i)<0){
        return -1;
    }

    return 0;
}
